﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;
using System.Drawing;

namespace CMS_GYM
{
    public partial class AutoGenericDataGridView : DataGridView
    {

        private bool _IsDataBinded;
        string m_DataColumns;
        string m_DataColumnsRaw;
        string m_DataColumnsTable;
        string m_DataConnection;
        string m_DBDateFormat;
        DataTable mMultipleComboboxDatatable;
        System.Data.DataTable dtValidation = new System.Data.DataTable("ValidationTable");

        public enum ValidationStyle
        {
            NumericInt, NumericDouble, Email, Date, Time, String248, String248NotNull, String4000, String30NotNull, NumericDoublePlus
        }

        public AutoGenericDataGridView()
        {
            InitializeComponent();
            this.IsDataBinded = false;
        }

        public AutoGenericDataGridView(IContainer container)

        {
            container.Add(this);

            InitializeComponent();
        }

        #region public method

        /// <summary>
        /// Author: HaLe
        /// Date:09/12/2010
        /// Dest: Get DataRow of current row
        /// </summary>
        /// <param name="dgv"></param>
        /// <returns></returns>
        public DataRow GetCurrentRow(DataGridView dgv)
        {
            DataRowView drv = null;
            try
            {
                if (dgv.CurrentRow == null) { return null; }
                if (dgv.CurrentRow.DataBoundItem == null) { return null; }
                drv = (DataRowView)dgv.CurrentRow.DataBoundItem;
            }
            catch
            {
                return null;
            }
            return drv.Row;
        }

        /// <summary>
        /// Author: HaLe
        /// Date:09/12/2010
        /// Dest: Get DataRowView of current row
        /// </summary>
        /// <param name="dgv"></param>
        /// <returns></returns>
        public DataRowView GetCurrentRowView(DataGridView dgv)
        {
            DataRowView drv = null;
            try
            {
                if (dgv.CurrentRow == null) { return null; }
                if (dgv.CurrentRow.DataBoundItem == null) { return null; }
                drv = (DataRowView)dgv.CurrentRow.DataBoundItem;
            }
            catch
            {
                return null;
            }
            return drv;
        }

        /// <summary>
        /// Author: HaLe
        /// Date:09/12/2010
        /// Dest: Kiểm tra "gridview row index" có phải là row mới hay không?
        /// Dựa trên  ID_TEMP_NAME       
        /// </summary>
        /// <param name="myGridView"></param>
        /// <param name="ID_TEMP_NAME"></param>
        /// <param name="rowIndex"></param>
        /// <returns></returns>
        public bool IsNewRow(DataGridView myGridView, string ID_TEMP_NAME, int rowIndex)
        {
            try
            {
                if (myGridView != null && !string.IsNullOrEmpty(ID_TEMP_NAME) && rowIndex > 0)
                {
                    DataGridViewCell cell = myGridView[ID_TEMP_NAME, rowIndex];
                    if (cell != null && (cell.FormattedValue == null || string.IsNullOrEmpty(cell.FormattedValue.ToString()))) return true;
                }
            }
            catch (Exception ex) { }

            return false;
        }

        /// <summary>
        /// Author: HaLe
        /// Date:09/12/2010
        /// Dest: Kiểm tra "gridview row index" có phải là row edit hay không?
        /// Dựa trên  ID_TEMP_NAME       
        /// </summary>
        /// <param name="myGridView"></param>
        /// <param name="ID_TEMP"></param>
        /// <param name="rowIndex"></param>
        /// <returns></returns>
        public bool IsRowEdit(DataGridView myGridView, string ID_TEMP_NAME, int rowIndex)
        {
            try
            {
                if (myGridView != null && !string.IsNullOrEmpty(ID_TEMP_NAME) && rowIndex > 0)
                {
                    DataGridViewCell cell = myGridView[ID_TEMP_NAME, rowIndex];
                    if (cell != null && cell.FormattedValue != null && cell.FormattedValue.ToString() != "") return true;
                }
            }
            catch (Exception ex) { }

            return false;
        }

        /// <summary>
        /// Author: HaLe
        /// Date:09/12/2010
        /// Dest: Kiểm tra ID có tồn tại trên gridview hay chưa
        /// </summary>
        /// <param name="myGridView"></param>
        /// <param name="ID_NAME"></param>
        /// <param name="ID_VALUE"></param>
        /// <returns></returns>
        public bool IsExistID(DataGridView myGridView, string ID_NAME, string ID_VALUE)
        {
            try
            {
                if (myGridView != null && !string.IsNullOrEmpty(ID_NAME) && !string.IsNullOrEmpty(ID_VALUE))
                {
                    DataGridViewRowCollection rows = myGridView.Rows;
                    foreach (DataGridViewRow row in rows)
                    {
                        if (row != myGridView.CurrentRow && row.Cells[ID_NAME].FormattedValue.ToString().ToLower() == ID_VALUE.ToLower()) return true;
                    }
                }
            }
            catch (Exception ex) { }

            return false;
        }

        public void SetFocusToEditCell(DataGridView myGridView, int rowIndex, string columnName)
        {
            if (myGridView == null || rowIndex < 0 || string.IsNullOrEmpty(columnName)) return;

            DataGridViewRow row = myGridView.Rows[rowIndex];

            myGridView.CurrentCell = row.Cells[columnName];
            myGridView.Focus();
            row.Cells[columnName].Selected = true;
            myGridView.BeginEdit(true);
        }

        #endregion

        #region New Properties

        /// <summary>
        /// HaLe
        /// Co de phan biet grid da binding data hay chua dua tren su kien mouse click
        /// Khi click chuot vao grid thi set gia tri la databinded
        /// </summary>
        public bool IsDataBinded
        {
            get { return _IsDataBinded; }
            set { _IsDataBinded = value; }
        }

        // [Browsable(false), EditorBrowsable(EditorBrowsableState.Always)] 
        [Browsable(true)]
        public string DataColumns
        {
            get
            {
                return m_DataColumnsRaw;
            }
            set
            {
                m_DataColumnsRaw = value;
                // m_DataColumns = value.Replace("%", "");
                // m_DataColumns = m_DataColumns.Replace("-", "");
            }
        }

        [Browsable(true)]
        public string DataColumnsTable
        {
            get
            {
                return m_DataColumnsTable;
            }
            set
            {
                m_DataColumnsTable = value;

            }
        }

        //public bool ExcelExportButton
        //{
        //    get
        //    {
        //        return m_ExcelExportButton;
        //    }
        //    set
        //    {
        //        m_ExcelExportButton = value;
        //        if (value == true)
        //        {
        //            if (btDown.Text != "->")
        //            {
        //                btDown.Text = "->";
        //                btDown.Location = new System.Drawing.Point(this.Location.X, this.Location.Y - 30);
        //                btDown.Click += new EventHandler(BtDownOnClick);
        //                this.Parent.Controls.Add(btDown);
        //            }
        //        }
        //        btDown.Visible = value;
        //     }
        //}

        //public void BtDownOnClick(object sender, EventArgs e)
        //{

        //}

        [Browsable(true)]
        public string DataConnection
        {
            get
            {
                return m_DataConnection;
            }
            set
            {
                m_DataConnection = value;
            }
        }

        [Browsable(true)]
        public string DBDateFormat
        {
            get
            {
                return m_DBDateFormat;
            }
            set
            {
                m_DBDateFormat = value;
            }
        }

        #endregion

        #region FillAll
        public void FillAll(DataTable table)
        {
            DataSource = table;
            table.Columns[0].ReadOnly = true;
        }
        #endregion

        #region AddValidation
        public void AddValidation(string ColumnName, bool BlankControl, ValidationStyle ValidationType, string ValidationMessage)
        {
            if (dtValidation.Columns.Count == 0)
            {
                dtValidation.Columns.Add("Name", Type.GetType("System.String"));
                dtValidation.Columns.Add("BlankControl", Type.GetType("System.Boolean"));
                dtValidation.Columns.Add("Validation", Type.GetType("System.Int16"));
                // ValidationType
                //dtValidation.Columns.Add("Validation", Type.GetType(ValidationType));
                dtValidation.Columns.Add("ValidationMessage", Type.GetType("System.String"));


            }
            System.Data.DataRow dr;
            dr = dtValidation.NewRow();
            dr[0] = ColumnName;
            dr[1] = BlankControl;
            dr[2] = ValidationType;
            dr[3] = ValidationMessage;
            dtValidation.Rows.Add(dr);

        }
        #endregion

        #region AddCombo
        public void AddCombo(string ColumnName, string HeaderText, DataTable SourceTable, string DisplayColumn, string ValueMember)
        {
            DataGridViewComboBoxColumn column_addcombo = new DataGridViewComboBoxColumn();
            column_addcombo.DataSource = SourceTable;
            column_addcombo.DisplayMember = DisplayColumn;
            column_addcombo.ValueMember = ValueMember;
            column_addcombo.DataPropertyName = ColumnName;
            column_addcombo.Name = ColumnName;
            column_addcombo.HeaderText = HeaderText;
            column_addcombo.AutoComplete = true;
            column_addcombo.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            column_addcombo.DisplayIndex = 0;
            int k = -1;
            for (int i = 0; i < Columns.Count; i++)
            {
                if (Columns[i].Name == ColumnName)
                {
                    column_addcombo.AutoSizeMode = Columns[i].AutoSizeMode;
                    column_addcombo.Width = Columns[i].Width;
                    column_addcombo.DefaultCellStyle = Columns[i].DefaultCellStyle;//;DataGridViewCellStyle { Alignment=MiddleCenter }
                    Columns.Remove(ColumnName);
                    k = i;
                    break;
                }
            }
            Columns.Add(column_addcombo);
            if (k != -1)
            {
                Columns[Columns.Count - 1].DisplayIndex = k;
            }

        }
        #endregion

        #region AddCalendar
        public void AddCalendar(string ColumnName, string HeaderText)
        {
            CalendarColumn column_addcalendar = new CalendarColumn();
            column_addcalendar.DataPropertyName = ColumnName;
            column_addcalendar.Name = ColumnName;
            column_addcalendar.HeaderText = HeaderText;
            int k = -1;
            for (int i = 0; i < Columns.Count; i++)
            {
                if (Columns[i].Name == ColumnName)
                {
                    column_addcalendar.AutoSizeMode = Columns[i].AutoSizeMode;
                    column_addcalendar.Width = Columns[i].Width;
                    column_addcalendar.DefaultCellStyle = Columns[i].DefaultCellStyle;//;DataGridViewCellStyle { Alignment=MiddleCenter }
                    Columns.Remove(ColumnName);
                    k = i;
                    break;
                }
            }
            Columns.Add(column_addcalendar);
            if (k != -1)
            {
                Columns[Columns.Count - 1].DisplayIndex = k;
            }
        }
        public void AddCalendarTime(string ColumnName, string HeaderText, string CustomFormat)
        {

            CalendarColumn column_addcalendar = new CalendarColumn();
            column_addcalendar.DataPropertyName = ColumnName;
            column_addcalendar.Name = ColumnName;
            column_addcalendar.HeaderText = HeaderText;
            int k = -1;
            for (int i = 0; i < Columns.Count; i++)
            {
                if (Columns[i].Name == ColumnName)
                {
                    column_addcalendar.AutoSizeMode = Columns[i].AutoSizeMode;
                    column_addcalendar.Width = Columns[i].Width;
                    //column_addcalendar.DefaultCellStyle = Columns[i].DefaultCellStyle;//;DataGridViewCellStyle { Alignment=MiddleCenter }
                    column_addcalendar.DefaultCellStyle.Format = CustomFormat;
                    Columns.Remove(ColumnName);
                    k = i;
                    break;
                }
            }
            Columns.Add(column_addcalendar);
            if (k != -1)
            {
                Columns[Columns.Count - 1].DisplayIndex = k;
            }
        }
        #endregion

        #region AddMultiCombo.
        public void AddMultiCombo(string ColumnName, string HeaderText, string SourceTable, string DisplayColumn, string ValueMember, string SWhere, System.Data.SqlClient.SqlConnection connectSQL)
        {
            try
            {
                if (Columns[ColumnName] != null)
                {
                    Columns.Remove(ColumnName);
                }

                if (connectSQL.State != ConnectionState.Open)
                {
                    connectSQL.Open();
                }

                string sql;

                sql = "Select " + DisplayColumn + " as LB," + ValueMember + " as VL from " + SourceTable + " " + SWhere;

                System.Data.SqlClient.SqlDataAdapter da_addcombo = new SqlDataAdapter(sql, connectSQL);

                System.Data.DataTable dt_addcombo;

                dt_addcombo = new System.Data.DataTable();

                da_addcombo.Fill(dt_addcombo);

                DataGridViewComboBoxColumn column_addcombo = new DataGridViewComboBoxColumn();

                column_addcombo.DataSource = dt_addcombo;

                column_addcombo.DisplayMember = "LB";

                column_addcombo.ValueMember = "VL";

                column_addcombo.DataPropertyName = ColumnName;

                column_addcombo.Name = ColumnName;

                column_addcombo.HeaderText = HeaderText;

                Columns.Add(column_addcombo);

                //int j, i;

                //string[] k;

                //char a;

                //a = ',';

                //k = m_DataColumns.Split(a);

                //i = k.Length - 1;

                //j = 0;

                //while ((j < i) && (k[j].IndexOf(ColumnName) == -1))
                //{

                //    j = j + 1;

                //}

                //Columns[i].DisplayIndex = j;


            }
            catch (System.Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                if (connectSQL.State == ConnectionState.Open)
                {
                    connectSQL.Close();
                }
            }
        }
        #endregion AddMultiCombo.

        #region Format Column temple
        public void FormatDefaulColumnsHead()
        {
            this.ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.True;
            this.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
            this.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            this.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
        }
        #endregion

        #region Format Column Fill
        public void FormatColumnFill(string ColumnName, string ColumnHeaderText, double PercentWith)
        {
            try
            {
                Columns[ColumnName].Width = Convert.ToInt32(this.Width * PercentWith);
                Columns[ColumnName].HeaderText = ColumnHeaderText;
                Columns[ColumnName].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                Columns[ColumnName].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public void FormatColumnsFillAll()
        {
            for (int i = 0; i < Columns.Count; i++)
            {
                Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                Columns[i].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
            }
        }
        #endregion

        #region AddMask
        public void AddMask(string ColumnName, string HeaderText, string MaskStr)
        {
            MaskedTextColumn column_addmask;
            column_addmask = new MaskedTextColumn();
            column_addmask.DataPropertyName = ColumnName;
            column_addmask.Name = ColumnName;
            column_addmask.HeaderText = HeaderText;
            // column_addmask.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.None;
            int k = -1;
            for (int i = 0; i < Columns.Count; i++)
            {
                if (Columns[i].Name == ColumnName)
                {
                    column_addmask.AutoSizeMode = Columns[i].AutoSizeMode;
                    column_addmask.Width = Columns[i].Width;
                    column_addmask.DefaultCellStyle = Columns[i].DefaultCellStyle;//;DataGridViewCellStyle { Alignment=MiddleCenter }
                    Columns.Remove(ColumnName);
                    k = i;
                    break;
                }
            }
            column_addmask.maskA(MaskStr);
            Columns.Add(column_addmask);
            if (k != -1)
            {
                Columns[Columns.Count - 1].DisplayIndex = k;

            }
        }
        #endregion

        #region Events
        /// <param name="e">A <see cref="T:System.Windows.Forms.DataGridViewCellCancelEventArgs"></see> that contains the event data.</param>
        protected override void OnCellBeginEdit(System.Windows.Forms.DataGridViewCellCancelEventArgs e)
        {
            //minhnb
            // CurrentRow.DefaultCellStyle.BackColor = System.Drawing.Color.LightGoldenrodYellow;
            base.OnCellBeginEdit(e);
        }


        /// <param name="e">A <see cref="T:System.Windows.Forms.DataGridViewCellEventArgs"></see> that contains the event data.</param>
        protected override void OnRowValidated(System.Windows.Forms.DataGridViewCellEventArgs e)
        {
            //minhnb
            //if (CurrentRow.DefaultCellStyle.BackColor == System.Drawing.Color.LightGoldenrodYellow)
            //{
            //  CurrentRow.DefaultCellStyle.BackColor = System.Drawing.Color.LightYellow;
            //Row_Save(e.RowIndex);
            base.OnRowValidated(e);
            //}
        }


        /// <param name="e">A <see cref="T:System.Windows.Forms.DataGridViewCellEventArgs"></see> that contains the event data.</param>
        protected override void OnCellEndEdit(System.Windows.Forms.DataGridViewCellEventArgs e)
        {
            //this.Rows[e.RowIndex].ErrorText = String.Empty;
            base.OnCellEndEdit(e);
        }

        /// <param name="e">A <see cref="T:System.Windows.Forms.DataGridViewCellValidatingEventArgs"></see> that contains the event data.</param>
        protected override void OnCellValidating(System.Windows.Forms.DataGridViewCellValidatingEventArgs e)
        {
            int j;
            j = dtValidation.Rows.Count - 1;
            if (j != -1)
            {
                bool check = false;
                System.Data.DataView dv = new System.Data.DataView(dtValidation, "Name='" + this.Columns[e.ColumnIndex].Name + "'", null, System.Data.DataViewRowState.CurrentRows);
                if (dv.Count == 1)
                {
                    if ((bool)dv[0][1])
                    {
                        if (String.IsNullOrEmpty(e.FormattedValue.ToString()))
                        {
                            this.Rows[e.RowIndex].ErrorText = "Empty Text";
                            e.Cancel = true;
                        }
                    }
                    if ((dv[0][2].ToString()) != "")
                    {
                        switch (dv[0][2].ToString())
                        {
                            case "0":
                                check = IsNumericInt(e.FormattedValue.ToString());
                                break;
                            case "1":
                                check = IsNumericDouble(e.FormattedValue.ToString());
                                break;
                            case "2":
                                check = IsEmail(e.FormattedValue.ToString());
                                break;
                            case "3":
                                check = IsDate(e.FormattedValue.ToString());
                                break;
                            case "4":
                                check = IsTime(e.FormattedValue.ToString());
                                break;
                            case "5":
                                check = IsLengthString248(e.FormattedValue.ToString(), false);
                                break;
                            case "6":
                                check = IsLengthString248(e.FormattedValue.ToString(), true);
                                break;
                            case "7":
                                check = IsLengthString4000(e.FormattedValue.ToString());
                                break;
                            case "8":
                                check = IsLengthString30(e.FormattedValue.ToString());
                                break;
                            case "9":
                                check = IsNumericDoublePlus(e.FormattedValue.ToString());
                                break;
                        }
                        if (check)
                        {
                            this.Rows[e.RowIndex].ErrorText = dv[0][3].ToString();
                            e.Cancel = check;
                        }
                        else
                        {
                            this.Rows[e.RowIndex].ErrorText = null;
                            e.Cancel = check;
                        }
                    }

                }
            }
            base.OnCellValidating(e);
        }

        protected override void OnCellMouseClick(DataGridViewCellMouseEventArgs e)
        {
            base.OnCellMouseClick(e);
        }

        protected override void OnMouseHover(EventArgs e)
        {
            if (!this.IsDataBinded) this.IsDataBinded = true;
            base.OnMouseHover(e);
        }

        //protected override bool ProcessDialogKey(Keys keyData)
        //{
        //    //if the key pressed is "return" then tell the datagridview to move to the next cell
        //    if (keyData == Keys.Enter)
        //    {
        //        MoveToNextCell();
        //        return true;
        //    }
        //    else
        //        return base.ProcessDialogKey(keyData);
        //}

        ///// <summary>
        ///// this function moves the focus to the next cell
        ///// </summary>
        public bool MoveToNextCell()
        {
            int CurrentColumn, CurrentRow;
            if (CurrentCell != null)
            {
                //get the current indicies of the cell
                CurrentColumn = this.CurrentCell.ColumnIndex;
                CurrentRow = this.CurrentCell.RowIndex;

                //if cell is at the end move it to the first cell of the next row
                //other with move it to the next cell
                if (CurrentColumn == DetailColumn())
                {
                    if (CurrentRow != this.Rows.Count - 1)
                    {
                        base.ProcessDataGridViewKey(new KeyEventArgs(Keys.Home));
                        base.ProcessDataGridViewKey(new KeyEventArgs(Keys.Down));
                    }
                    else
                    {
                        //base.ProcessDataGridViewKey(new KeyEventArgs(Keys.Home));
                        SendKeys.Send("{TAB}");
                    }
                }
                else
                    base.ProcessDataGridViewKey(new KeyEventArgs(Keys.Right));
                return true;
            }
            else
            {
                return false;
            }

        }

        private int DetailColumn()
        {
            int MaxColumn=-1;
            for (int i = 0; i < this.Columns.Count; i++)
            {
                if (this.Columns[i].Visible)
                {
                    MaxColumn = i;
                }
            }
            return MaxColumn;
        }

        protected override bool ProcessDialogKey(Keys keyData)
        {
            Keys key = (keyData & Keys.KeyCode);
            if (key == Keys.Enter)
            {
                int CurrentColumn, CurrentRow;
                if (CurrentCell != null)
                {
                    //get the current indicies of the cell
                    CurrentColumn = this.CurrentCell.ColumnIndex;
                    CurrentRow = this.CurrentCell.RowIndex;

                    //if cell is at the end move it to the first cell of the next row
                    //other with move it to the next cell
                    if (CurrentColumn == DetailColumn() )
                    {
                        if (CurrentRow != this.Rows.Count - 1)
                        {
                            base.ProcessDataGridViewKey(new KeyEventArgs(Keys.Home));
                            base.ProcessDataGridViewKey(new KeyEventArgs(Keys.Down));
                        }
                        else
                        {
                            //return this.ProcessRightKey(keyData);
                            SendKeys.Send("{TAB}");
                        }
                        return true;
                    }
                    else
                        return this.ProcessRightKey(keyData);

                }
                return this.ProcessRightKey(keyData);
            }
            return base.ProcessDialogKey(keyData);
        }
        protected override bool ProcessDataGridViewKey(KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                //return this.ProcessRightKey(e.KeyData);
                return MoveToNextCell();
            }
            return base.ProcessDataGridViewKey(e);
        }

        #endregion

        #region ValidationControls
        private bool IsNumericDouble(object ValueToCheck)
        {
            double Dummy = 0;
            return !double.TryParse(ValueToCheck.ToString(), System.Globalization.NumberStyles.Any, null, out Dummy);
        }

        private bool IsNumericDoublePlus(object ValueToCheck)
        {
            double Dummy = 0;
            if (double.TryParse(ValueToCheck.ToString(), System.Globalization.NumberStyles.Any, null, out Dummy))
            {
                if (Dummy >= 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            else
            {
                return true;
            }
        }

        private bool IsNumericInt(object ValueToCheck)
        {
            int Dummy = 0;
            return !int.TryParse(ValueToCheck.ToString(), System.Globalization.NumberStyles.Any, null, out Dummy);
        }

        private bool IsEmail(object ValueToCheck)
        {
            return !System.Text.RegularExpressions.Regex.IsMatch(ValueToCheck.ToString(), @"^([\w-]+\.)*?[\w-]+@[\w-]+\.([\w-]+\.)*?[\w]+$");
        }

        private bool IsDate(object ValueToCheck)
        {
            return !System.Text.RegularExpressions.Regex.IsMatch(ValueToCheck.ToString(), @"^[0-2]?[1-9](/|-|.)[0-3]?[0-9](/|-|.)[1-2][0-9][0-9][0-9]$");

        }
        private bool IsTime(object ValueToCheck)
        {
            if (System.Text.RegularExpressions.Regex.IsMatch(ValueToCheck.ToString(), @"^[0-2]?[0-9](:)[0-5]?[0-9]$") == true)
            {
                if (ConvertToTime(ValueToCheck.ToString()) != -1)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            else
            {
                return true;
            }

        }
        private bool IsLengthString248(object ValueToCheck, bool isNotNull)
        {
            if (isNotNull == false)
            {
                if (ValueToCheck.ToString().Length <= 248)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            else
            {
                if (ValueToCheck.ToString().Length > 0 && ValueToCheck.ToString().Length <= 248)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }

        }
        private bool IsLengthString30(object ValueToCheck)
        {
            if (ValueToCheck.ToString().Length > 0 && ValueToCheck.ToString().Length <= 30)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        private bool IsLengthString4000(object ValueToCheck)
        {
            if (ValueToCheck.ToString().Length <= 4000)
            {
                return false;
            }
            else
            {
                return true;
            }

        }
        private int ConvertToTime(string str)
        {
            String split = ":";
            String[] strsplit ={ split };
            String[] M = str.Split(strsplit, StringSplitOptions.None);
            //Checking format : HH:MM
            if (M.Length == 2)
            {
                if (M[0].Length > 0 && M[0].Length < 3)
                {
                    if (M[1].Length > 0 && M[0].Length < 3)
                    {
                        int h = int.Parse(M[0]);
                        int m = int.Parse(M[1]);
                        if (h >= 0 && m >= 0)
                        {
                            int Duration = (h * 60) + m;
                            //checking time
                            if (Duration >= 0 && Duration < 1440)
                            {
                                return Duration;
                            }
                        }
                    }
                }

            }
            return -1;
        }
        #endregion

        #region CalendarColumn

        public class CalendarColumn : DataGridViewColumn
        {
            public CalendarColumn()
                : base(new CalendarCell())
            {
            }

            public override DataGridViewCell CellTemplate
            {
                get
                {
                    return base.CellTemplate;
                }
                set
                {
                    if (value != null &&
                        !value.GetType().IsAssignableFrom(typeof(CalendarCell)))
                    {
                        throw new InvalidCastException("Must be a CalendarCell");
                    }
                    base.CellTemplate = value;
                }
            }
        }

        public class CalendarCell : DataGridViewTextBoxCell
        {

            public CalendarCell()
                : base()
            {
                // Use the short date format.
                this.Style.Format = "dd/MM/yyyy";
            }

            public override void InitializeEditingControl(int rowIndex, object
                initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
            {
                // Set the value of the editing control to the current cell value.
                base.InitializeEditingControl(rowIndex, initialFormattedValue,
                    dataGridViewCellStyle);
                CalendarEditingControl ctl =
                    DataGridView.EditingControl as CalendarEditingControl;

                if (this.Value != System.DBNull.Value)
                {
                    ctl.Value = (DateTime)this.Value;
                }

            }

            public override Type EditType
            {
                get
                {
                    // Return the type of the editing contol that CalendarCell uses.
                    return typeof(CalendarEditingControl);
                }
            }

            public override Type ValueType
            {
                get
                {
                    // Return the type of the value that CalendarCell contains.
                    return typeof(DateTime);
                }
            }

            public override object DefaultNewRowValue
            {
                get
                {
                    // Use the current date and time as the default value.
                    return DateTime.Now;
                }
            }
        }

        class CalendarEditingControl : DateTimePicker, IDataGridViewEditingControl
        {
            DataGridView dataGridView;
            private bool valueChanged = false;
            int rowIndex;

            public CalendarEditingControl()
            {
                this.Format = DateTimePickerFormat.Custom;
                this.CustomFormat = "dd/MM/yyyy";
            }

            // Implements the IDataGridViewEditingControl.EditingControlFormattedValue 
            // property.
            public object EditingControlFormattedValue
            {
                get
                {
                    return this.Value.ToShortDateString();
                }
                set
                {
                    if (value is String)
                    {
                        this.Value = DateTime.Parse((String)value);
                    }
                }
            }

            // Implements the 
            // IDataGridViewEditingControl.GetEditingControlFormattedValue method.
            public object GetEditingControlFormattedValue(
                DataGridViewDataErrorContexts context)
            {
                return EditingControlFormattedValue;
            }

            // Implements the 
            // IDataGridViewEditingControl.ApplyCellStyleToEditingControl method.
            public void ApplyCellStyleToEditingControl(
                DataGridViewCellStyle dataGridViewCellStyle)
            {
                this.Font = dataGridViewCellStyle.Font;
                this.CalendarForeColor = dataGridViewCellStyle.ForeColor;
                this.CalendarMonthBackground = dataGridViewCellStyle.BackColor;
            }

            // Implements the IDataGridViewEditingControl.EditingControlRowIndex 
            // property.
            public int EditingControlRowIndex
            {
                get
                {
                    return rowIndex;
                }
                set
                {
                    rowIndex = value;
                }
            }

            // Implements the IDataGridViewEditingControl.EditingControlWantsInputKey 
            // method.
            public bool EditingControlWantsInputKey(
                Keys key, bool dataGridViewWantsInputKey)
            {
                // Let the DateTimePicker handle the keys listed.
                switch (key & Keys.KeyCode)
                {
                    case Keys.Left:
                    case Keys.Up:
                    case Keys.Down:
                    case Keys.Right:
                    case Keys.Home:
                    case Keys.End:
                    case Keys.PageDown:
                    case Keys.PageUp:
                        return true;
                    default:
                        return false;
                }
            }

            // Implements the IDataGridViewEditingControl.PrepareEditingControlForEdit 
            // method.
            public void PrepareEditingControlForEdit(bool selectAll)
            {
                // No preparation needs to be done.
            }

            // Implements the IDataGridViewEditingControl
            // .RepositionEditingControlOnValueChange property.
            public bool RepositionEditingControlOnValueChange
            {
                get
                {
                    return false;
                }
            }

            // Implements the IDataGridViewEditingControl
            // .EditingControlDataGridView property.
            public DataGridView EditingControlDataGridView
            {
                get
                {
                    return dataGridView;
                }
                set
                {
                    dataGridView = value;
                }
            }

            // Implements the IDataGridViewEditingControl
            // .EditingControlValueChanged property.
            public bool EditingControlValueChanged
            {
                get
                {
                    return valueChanged;
                }
                set
                {
                    valueChanged = value;
                }
            }

            // Implements the IDataGridViewEditingControl
            // .EditingPanelCursor property.
            public Cursor EditingPanelCursor
            {
                get
                {
                    return base.Cursor;
                }
            }

            protected override void OnValueChanged(EventArgs eventargs)
            {
                // Notify the DataGridView that the contents of the cell
                // have changed.
                valueChanged = true;
                this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
                base.OnValueChanged(eventargs);
            }
        }

        #endregion

        #region MaskColumn

        public class mskn
        {
            static public string msk = "";
        }


        public class MaskedTextColumn : DataGridViewColumn
        {
            public MaskedTextColumn()
                : base(new MaskedTextCell())
            { }

            public void maskA(string m)
            {
                mskn.msk = m;
            }


            public override DataGridViewCell CellTemplate
            {
                get
                {
                    return base.CellTemplate;
                }
                set
                {
                    if (value != null &&
                        !value.GetType().IsAssignableFrom(typeof(MaskedTextCell)))
                    {
                        throw new InvalidCastException("Must be a MaskedTextCell");
                    }
                    base.CellTemplate = value;
                }
            }



        }

        public class MaskedTextCell : DataGridViewTextBoxCell
        {
            public MaskedTextCell()
                : base()
            {
                this.Style.ForeColor = System.Drawing.Color.RoyalBlue;

            }




            public override void InitializeEditingControl(int rowIndex, object
                initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
            {
                // Set the value of the editing control to the current cell value.
                base.InitializeEditingControl(rowIndex, initialFormattedValue,
                    dataGridViewCellStyle);
                MaskedTextEditingControl ctl = DataGridView.EditingControl as MaskedTextEditingControl;
                ctl.Text = (string)this.Value;


            }

            public override Type EditType
            {
                get
                {
                    return typeof(MaskedTextEditingControl);
                }
            }

            public override Type ValueType
            {
                get
                {
                    return typeof(string);
                }
            }



            class MaskedTextEditingControl : MaskedTextBox, IDataGridViewEditingControl
            {
                DataGridView dataGridView;
                private bool valueChanged;
                int rowIndex;

                public MaskedTextEditingControl()
                {
                    this.Mask = mskn.msk;
                }


                public object EditingControlFormattedValue
                {
                    get
                    {
                        return this.Text;
                    }
                    set
                    {
                        if (value is String)
                        {
                            this.Text = (String)value;

                        }
                    }
                }

                public object GetEditingControlFormattedValue(
                    DataGridViewDataErrorContexts context)
                {
                    return EditingControlFormattedValue;
                }

                public void ApplyCellStyleToEditingControl(
                    DataGridViewCellStyle dataGridViewCellStyle)
                {
                    this.Font = dataGridViewCellStyle.Font;
                }

                // Implements the IDataGridViewEditingControl.EditingControlRowIndex 
                // property.
                public int EditingControlRowIndex
                {
                    get
                    {
                        return rowIndex;
                    }
                    set
                    {
                        rowIndex = value;
                    }
                }

                public bool EditingControlWantsInputKey(
                    Keys key, bool dataGridViewWantsInputKey)
                {
                    switch (key & Keys.KeyCode)
                    {
                        case Keys.Left:
                        case Keys.Up:
                        case Keys.Down:
                        case Keys.Right:
                        case Keys.Home:
                        case Keys.End:
                        case Keys.PageDown:
                        case Keys.PageUp:
                            return true;
                        default:
                            return false;
                    }
                }

                public void PrepareEditingControlForEdit(bool selectAll)
                {
                    // No preparation needs to be done.
                }

                public bool RepositionEditingControlOnValueChange
                {
                    get
                    {
                        return false;
                    }
                }

                public DataGridView EditingControlDataGridView
                {
                    get
                    {
                        return dataGridView;
                    }
                    set
                    {
                        dataGridView = value;
                    }
                }

                public bool EditingControlValueChanged
                {
                    get
                    {
                        return valueChanged;
                    }
                    set
                    {
                        valueChanged = value;
                    }
                }


                public Cursor EditingPanelCursor
                {
                    get
                    {
                        return base.Cursor;
                    }
                }


                /// <param name="e">An <see cref="T:System.EventArgs"></see> that contains the event data.</param>
                protected override void OnLeave(System.EventArgs e)
                {
                    valueChanged = true;
                    this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
                    base.OnTextChanged(e);
                }

            }


        }


        #endregion

        #region MultipleComboboxColumn



        //public class Mul
        //{
        //    static public DataTable mMultipleComboboxDatatable;
        //    static public int mSelectedIndex;
        //}

        //public class MultipleComboboxColumn : DataGridViewColumn
        //{
        //    public MultipleComboboxColumn()
        //        : base(new MultipleComboboxCell())
        //    {
        //    }

        //    public override DataGridViewCell CellTemplate
        //    {
        //        get
        //        {
        //            return base.CellTemplate;
        //        }
        //        set
        //        {
        //            if (value != null &&
        //                !value.GetType().IsAssignableFrom(typeof(MultipleComboboxCell)))
        //            {
        //                throw new InvalidCastException("Must be a MultipleComboboxCell");
        //            }
        //            base.CellTemplate = value;
        //        }
        //    }
        //    public void FillMultipleCombobox(DataTable dtDatatable)
        //    {
        //        Mul.mMultipleComboboxDatatable = dtDatatable;

        //    }
        //}

        public class MultipleComboboxCell : DataGridViewTextBoxCell
        {
            public MultipleComboboxCell()
                : base()
            {
                //minhnb
                //this.Style.ForeColor = System.Drawing.Color.Transparent;

            }



            public override void InitializeEditingControl(int rowIndex, object
                initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
            {
                // Set the value of the editing control to the current cell value.
                base.InitializeEditingControl(rowIndex, initialFormattedValue,
                    dataGridViewCellStyle);
                MultipleComboboxEditingControl ctl = DataGridView.EditingControl as MultipleComboboxEditingControl;
                //Int32 a;
                //a=Convert.ToString(this.Value);
                ctl.Text = Convert.ToString(this.Value);


            }

            public override Type EditType
            {
                get
                {
                    return typeof(MultipleComboboxEditingControl);
                }
            }

            public override Type ValueType
            {
                get
                {
                    return typeof(string);
                }
            }



            class MultipleComboboxEditingControl : ComboBox, IDataGridViewEditingControl
            {
                DataGridView dataGridView;
                private bool valueChanged;
                int rowIndex;

                public MultipleComboboxEditingControl()
                {
                    //this.Mask = mskn.msk;
                }

                //public void FillMultipleCombobox()
                //{
                //    DataSource = Mul.mMultipleComboboxDatatable;
                //}


                public object EditingControlFormattedValue
                {
                    get
                    {
                        return this.Text;
                    }
                    set
                    {
                        if (value is String)
                        {
                            this.Text = (String)value;

                        }
                    }
                }

                public object GetEditingControlFormattedValue(
                    DataGridViewDataErrorContexts context)
                {
                    return EditingControlFormattedValue;
                }

                public void ApplyCellStyleToEditingControl(
                    DataGridViewCellStyle dataGridViewCellStyle)
                {
                    this.Font = dataGridViewCellStyle.Font;
                }

                // Implements the IDataGridViewEditingControl.EditingControlRowIndex 
                // property.
                public int EditingControlRowIndex
                {
                    get
                    {
                        return rowIndex;
                    }
                    set
                    {
                        rowIndex = value;
                    }
                }

                public bool EditingControlWantsInputKey(
                    Keys key, bool dataGridViewWantsInputKey)
                {
                    switch (key & Keys.KeyCode)
                    {
                        case Keys.Left:
                        case Keys.Up:
                        case Keys.Down:
                        case Keys.Right:
                        case Keys.Home:
                        case Keys.End:
                        case Keys.PageDown:
                        case Keys.PageUp:
                            return true;
                        default:
                            return false;
                    }
                }

                public void PrepareEditingControlForEdit(bool selectAll)
                {
                    // No preparation needs to be done.
                }

                public bool RepositionEditingControlOnValueChange
                {
                    get
                    {
                        return false;
                    }
                }

                public DataGridView EditingControlDataGridView
                {
                    get
                    {
                        return dataGridView;
                    }
                    set
                    {
                        dataGridView = value;
                    }
                }

                public bool EditingControlValueChanged
                {
                    get
                    {
                        return valueChanged;
                    }
                    set
                    {
                        valueChanged = value;
                    }
                }


                public Cursor EditingPanelCursor
                {
                    get
                    {
                        return base.Cursor;
                    }
                }




                /// <param name="e">An <see cref="T:System.EventArgs"></see> that contains the event data.</param>
                protected override void OnLeave(System.EventArgs e)
                {
                    valueChanged = true;
                    this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
                    base.OnTextChanged(e);
                }




                //protected override void OnSelectedIndexChanged(EventArgs e)
                //{
                //    // base.OnSelectedIndexChanged(e);
                //    ////this.SelectedIndex = popup.selectedRowValue;
                //    ////Mul.mSelectedIndex = popup.selectedRowValue;
                // //   SelectedIndex = popup.selectedRowValue;
                //    Mul.mSelectedIndex = 2;
                //}

                //protected override void OnSelectedIndexChanged1(EventArgs e)
                //{
                //    // base.OnSelectedIndexChanged(e);
                //    ////this.SelectedIndex = popup.selectedRowValue;
                //    Mul.mSelectedIndex = 1;
                //    //SelectedIndex = popup.selectedRowValue;
                //}



                protected override void OnClick(EventArgs e)
                {
                    // base.OnClick(e);
                }

                protected override void OnDoubleClick(EventArgs e)
                {
                    //base.OnDoubleClick(e);
                }

                protected override void OnDisplayMemberChanged(EventArgs e)
                {
                    // base.OnDisplayMemberChanged(e);
                }

                protected override void OnDataSourceChanged(EventArgs e)
                {
                    //                    base.OnDataSourceChanged(e);
                }

                protected override void OnMouseClick(MouseEventArgs e)
                {
                    //base.OnMouseClick(e);
                }


                protected override void OnMouseDoubleClick(MouseEventArgs e)
                {
                    // 	         base.OnMouseDoubleClick(e);
                }

                protected override void OnEnter(EventArgs e)
                {
                    //base.OnEnter(e);
                }

                protected override void OnGotFocus(EventArgs e)
                {
                    //base.OnGotFocus(e);
                }

                protected override void OnSelectedItemChanged(EventArgs e)
                {
                    //base.OnSelectedItemChanged(e);
                }

                protected override void OnSelectedValueChanged(EventArgs e)
                {
                    //base.OnSelectedValueChanged(e);
                }

                protected override void OnSelectionChangeCommitted(EventArgs e)
                {
                    //base.OnSelectionChangeCommitted(e);
                }


                //                protected override void OnDropDown(System.EventArgs e)
                //                {
                //                    Form parent = this.FindForm();
                //                    popupForm popup = new popupForm();
                //                    popup.fillGrid(Mul.mMultipleComboboxDatatable);
                //                    popup.Location = new Point(this.Left + parent.Left + this.Parent.Left + this.Parent.Parent.Left + 4, parent.Top + this.Parent.Parent.Top + this.Parent.Top + this.Height + 4);
                //                    //popup.DatagridViewDblClick += new SelectedIndexChangeEvent(OnSelectedIndexChanged);
                //                    //popup.dataGridView1.DoubleClick += new SelectedIndexChangeEvent(OnSelectedIndexChanged);
                //                   // popup.dataGridView1.DoubleClick += new System.EventHandler(this.OnSelectedIndexChanged1);

                //                    DataSource = Mul.mMultipleComboboxDatatable;
                //                    DisplayMember = "LB";
                //                    ValueMember = "VL";
                //                    popup.Show();
                //                    //popup.ShowDialog();

                //                    ////this.SelectedIndex = popup.selectedRowValue;
                //                    ////Mul.mSelectedIndex = popup.selectedRowValue;
                //                    //SelectedIndex = popup.selectedRowValue;
                //                    //this.Height = 0;
                //                    //// this.SelectedValue = popup.selectedRowValue;
                ///*
                //                    if (this.dataTable != null || this.dataRows != null)
                //                    {
                //                        popupForm popup = new popupForm(this.dataTable, ref this.selectedRow, columnsToDisplay);
                //                        popup.AfterRowSelectEvent += new AfterRowSelectEventHandler(MultiColumnComboBox_AfterSelectEvent);
                //                        popup.Location = new Point(parent.Left + this.Left + 4, parent.Top + this.Bottom + this.Height);
                //                        popup.Show();
                //                        if (popup.SelectedRow != null)
                //                        {
                //                            try
                //                            {
                //                                this.selectedRow = popup.SelectedRow;
                //                                this.displayValue = popup.SelectedRow[this.displayMember].ToString();
                //                                this.Text = this.displayValue;
                //                            }
                //                            catch (Exception e2)
                //                            {
                //                                MessageBox.Show(e2.Message, "Error");
                //                            }
                //                        }
                //                        if (AfterSelectEvent != null)
                //                            AfterSelectEvent();
                //                    }
                // */
                //                  //  base.OnDropDown(e);
                //                }

            }


        }

        #endregion

        #region DataGridViewDisableTextBoxCell

        public class DataGridViewDisableTextBoxColumn : DataGridViewColumn
        {
            public DataGridViewDisableTextBoxColumn()
                : base(new DataGridViewDisableTextBoxCell())
            {
            }

            public override DataGridViewCell CellTemplate
            {
                get
                {
                    return base.CellTemplate;
                }
                set
                {
                    if (value != null &&
                        !value.GetType().IsAssignableFrom(typeof(DataGridViewDisableTextBoxCell)))
                    {
                        throw new InvalidCastException("Must be a DataGridViewDisableTextBoxCell");
                    }
                    base.CellTemplate = value;
                }
            }
        }

        public class DataGridViewDisableTextBoxCell : DataGridViewTextBoxCell
        {

            private bool enabledValue;
            /// <summary>
            /// This property decides whether the checkbox should be shown checked or unchecked.
            /// </summary>
            public bool Enabled
            {
                get
                {
                    return enabledValue;
                }
                set
                {
                    enabledValue = value;
                }
            }
            /// Override the Clone method so that the Enabled property is copied.
            public override object Clone()
            {
                DataGridViewDisableTextBoxCell cell =
                    (DataGridViewDisableTextBoxCell)base.Clone();
                cell.Enabled = this.Enabled;
                return cell;
            }


            public DataGridViewDisableTextBoxCell()
                : base()
            {

            }
            /// <summary>
            /// Override the Paint method to show the disabled checked/unchecked datagridviewcheckboxcell.
            /// </summary>
            /// <param name="graphics"></param>
            /// <param name="clipBounds"></param>
            /// <param name="cellBounds"></param>
            /// <param name="rowIndex"></param>
            /// <param name="elementState"></param>
            /// <param name="value"></param>
            /// <param name="formattedValue"></param>
            /// <param name="errorText"></param>
            /// <param name="cellStyle"></param>
            /// <param name="advancedBorderStyle"></param>
            /// <param name="paintParts"></param>
            protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds,
                int rowIndex, DataGridViewElementStates elementState, object value,
                object formattedValue, string errorText, DataGridViewCellStyle cellStyle,
                DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
            {
                //base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);

                SolidBrush cellBackground = new SolidBrush(cellStyle.BackColor);
                graphics.FillRectangle(cellBackground, cellBounds);
                cellBackground.Dispose();
                PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle);
                Rectangle checkBoxArea = cellBounds;
                Rectangle buttonAdjustment = this.BorderWidths(advancedBorderStyle);
                checkBoxArea.X += buttonAdjustment.X;
                checkBoxArea.Y += buttonAdjustment.Y;

                checkBoxArea.Height -= buttonAdjustment.Height;
                checkBoxArea.Width -= buttonAdjustment.Width;
                Point drawInPoint = new Point(cellBounds.X + cellBounds.Width / 2 - 7, cellBounds.Y + cellBounds.Height / 2 - 7);

                if (this.enabledValue)
                {
                    //CheckBoxRenderer.DrawCheckBox(graphics, drawInPoint, System.Windows.Forms.VisualStyles.CheckBoxState.CheckedDisabled);
                    TextBoxRenderer.DrawTextBox(graphics, checkBoxArea, System.Windows.Forms.VisualStyles.TextBoxState.Disabled);
                }
                else
                {
                    //CheckBoxRenderer.DrawCheckBox(graphics, drawInPoint, System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedDisabled);
                    TextBoxRenderer.DrawTextBox(graphics, checkBoxArea, System.Windows.Forms.VisualStyles.TextBoxState.Normal);
                }

            }

        }
        #endregion

        #region Overrise

        protected override void OnRowPostPaint(DataGridViewRowPostPaintEventArgs e)
        {
            base.OnRowPostPaint(e);
            // get the row number in leading zero format, 
            //  where the width of the number = the width of the maximum number
            int RowNumWidth = this.RowCount.ToString().Length;
            StringBuilder RowNumber = new StringBuilder(RowNumWidth);
            RowNumber.Append(e.RowIndex + 1);
            while (RowNumber.Length < RowNumWidth)
                RowNumber.Insert(0, "0");

            // get the size of the row number string
            SizeF Sz = e.Graphics.MeasureString(RowNumber.ToString(), this.Font);

            // adjust the width of the column that contains the row header cells 
            if (this.RowHeadersWidth < (int)(Sz.Width + 20)) 
               this.RowHeadersWidth = (int)(Sz.Width + 20);

            // draw the row number
            e.Graphics.DrawString(
               RowNumber.ToString(), 
               this.Font, 
                SystemBrushes.ControlText, 
               e.RowBounds.Location.X + 15, 
               e.RowBounds.Location.Y + ((e.RowBounds.Height - Sz.Height) / 2));
        } //end OnRowPostPaint method

        #endregion

        public void SetRowNumeber(int RowNumber)
        {
            if (this.ColumnCount > 0 && RowNumber > 0 )
            {
                object[] obj = new object[ColumnCount];
                for (int i = 0; i < ColumnCount; i++)
                {
                    obj[i] = null;
                }
                for (int i = 0; i < RowNumber; i++)
                {
                    this.Rows.Add(obj); 
                }
            }
        }

    }

}
